package com.example.edu.Config;

import co.elastic.clients.elasticsearch.ElasticsearchClient;
import co.elastic.clients.json.jackson.JacksonJsonpMapper;
import co.elastic.clients.transport.rest_client.RestClientTransport;

import org.apache.http.HttpHost;
import org.elasticsearch.client.RestClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;

import static com.example.edu.Utils.MyConstants.*;

@Configuration
@EnableAsync
public class ElasticsearchConfig {

    @Bean
    public ElasticsearchClient elasticsearchClient() {
        // 创建 RestClient
        RestClient restClient = RestClient.builder(new HttpHost(ES_ADDRESS, ES_PORT, ES_CONNECT_SCHEME)).build();

        // 创建 Transport
        RestClientTransport transport = new RestClientTransport(restClient, new JacksonJsonpMapper());

        // 返回 ElasticsearchClient 实例
        return new ElasticsearchClient(transport);
    }
}
